<script >


import Ps from "ee-core/ps";
import {ipcApiRoute} from "@/api/main";
// import $ipc from "lodash";
import { ipc } from '@/utils/ipcRenderer';
let this1;
// import exe  from "child_process";
// const exe = window.require('child_process');
const exe  = require("child_process");
// 子进程名称
let frpProcess = null;
// 开始连接frp
let softwarePath = '';

export default {
  name: "fastMapping",
  created() {
    this1 = this
  },
  mounted() {
    // 读取缓存
    let proxyInfo = localStorage.getItem("proxyInfo") || "";
    if (proxyInfo) {
      this1.form = JSON.parse(proxyInfo);
    }
  },
  data(){
    return{
      proxyList: [
        {
          label: "TCP",
          value: "tcp",
        },
        {
          label: "HTTP",
          value: "http",
        },
      ],
      form:{
      },
      option:{
        submitText: "开始连接",
        emptyBtn: false,
        emptyText: "关闭连接",
        emptyIcon: "close",
        column: [
          {
            label: "代理节点",
            span: 24,
            prop: "proxyName",
            type: "select",
            value: "1",
            dicData: [
              {
                label: "上海阿里云服务器(限速10-30/mbps)",
                value: "1",
              },
              {
                label: "美国服务器(受各种限制阻断,不稳定,但不限速1g/mbps)",
                value: "2",
              },
              {
                label: "四川-腾讯服务器(限速20-50/mbps)",
                value: "3",
              },
            ],
            change: function ({value}) {
              let remoteDomain  = this1.findObject(this1.option.column,"remoteDomain");
              console.log(value)
              console.log(remoteDomain)
              if (value === "1") {
                remoteDomain.append = ".frp.chrelyonly.cn";
              } else  if (value === "2") {
                remoteDomain.append = ".frp.chrelyonly.cf";
              } else  if (value === "3") {
                remoteDomain.append = ".frp.tx.chrelyonly.cn";
              }
            },
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "请选择代理节点",
                trigger: "blur",
              },
            ],
          },
          {
            label: "代理协议",
            prop: "proxyType",
            type: "select",
            value: "http",
            span: 24,
            dicData: [
              {
                label: "TCP模式",
                value: "tcp",
              },
              {
                label: "HTTP模式",
                value: "http",
              },
              {
                label: "web服务器模式",
                value: "3",
              },
              {
                label: "反代目标域名模式",
                value: "4",
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "请选择代理协议",
                trigger: "blur",
              },
            ],
            change: function ({value}) {
              if (value === "tcp") {
                this1.findObject(this1.option.column,"remotePort").display = true;
                this1.findObject(this1.option.column,"remoteDomain").display = false;
                this1.findObject(this1.option.column,"domain").display = false;
                this1.findObject(this1.option.column,"dir").display = false;
                this1.findObject(this1.option.column,"tcpOrHttp").display = false;
                this1.findObject(this1.option.column,"proxyAddr").display = true;
                this1.findObject(this1.option.column,"proxyPort").display = true;
              } else  if (value === "http") {
                this1.findObject(this1.option.column,"remotePort").display = false;
                this1.findObject(this1.option.column,"remoteDomain").display = true;
                this1.findObject(this1.option.column,"domain").display = false;
                this1.findObject(this1.option.column,"dir").display = false;
                this1.findObject(this1.option.column,"tcpOrHttp").display = false;
                this1.findObject(this1.option.column,"proxyAddr").display = true;
                this1.findObject(this1.option.column,"proxyPort").display = true;
              } else  if (value === "3") {
                this1.findObject(this1.option.column,"dir").display = true;
                this1.findObject(this1.option.column,"domain").display = false;
                this1.findObject(this1.option.column,"tcpOrHttp").display = true;
                this1.findObject(this1.option.column,"proxyAddr").display = false;
                this1.findObject(this1.option.column,"proxyPort").display = false;
              } else  if (value === "4") {
                this1.findObject(this1.option.column,"domain").display = true;
                this1.findObject(this1.option.column,"dir").display = false;
                this1.findObject(this1.option.column,"remoteDomain").display = true;
                this1.findObject(this1.option.column,"tcpOrHttp").display = false;
                this1.findObject(this1.option.column,"remotePort").display = false;
                this1.findObject(this1.option.column,"proxyAddr").display = false;
                this1.findObject(this1.option.column,"proxyPort").display = false;
              }
            }
          },
          {
            label: "代理地址",
            span: 24,
            prop: "proxyAddr",
            display: true,
            value: "127.0.0.1",
            rules: [
              {
                required: true,
                message: "请填写代理地址",
                trigger: "blur",
              },
            ],
          },
          {
            label: "代理端口",
            span: 24,
            prop: "proxyPort",
            placeholder: "0-65535(需要穿透的端口)",
            type: "number",
            display: true,
            value: 80,
            rules: [
              {
                required: true,
                message: "请填写代理端口",
                trigger: "blur",
              },
            ],
          },

          {
            label: "本地共享地址",
            prop: "dir",
            span: 24,
            placeholder: "本地共享目录 例如: D:/share 默认 ./",
            type:"input",
            value: "./",
            display: false,
          },

          {
            label: "反代目标地址",
            prop: "domain",
            placeholder: "本地共享目录 例如: www.baidu.com ",
            span: 24,
            type:"input",
            display: false,
            rules: [
              {
                required: true,
                message: "请填写反代目标地址",
                trigger: "blur",
              },
            ],
          },
          {
            label: "需要域名",
            span: 24,
            prop: "tcpOrHttp",
            type: "switch",
            dataType:"string",
            value: "0",
            dicData: [
              {
                label: "否",
                value: "0",
              },
              {
                label: "是",
                value: "1",
              },
            ],
            props:{
              label: "label",
              value: "value",
            },
            display: false,
            change: function ({value}) {
              if (value === "1") {
                this1.findObject(this1.option.column,"remoteDomain").display = true;
                this1.findObject(this1.option.column,"remotePort").display = false;
              } else  if (value === "0") {
                this1.findObject(this1.option.column,"remoteDomain").display = false;
                this1.findObject(this1.option.column,"remotePort").display = true;
              }
            }
          },
          {
            label: "重定向端口",
            span: 24,
            placeholder: "50000-60000(服务器开放的端口)",
            prop: "remotePort",
            value: "0",
            type: "number",
            display: false,
          },
          {
            label: "重定向域名",
            span: 24,
            placeholder: "提供自定义免费域名",
            tip: "提供(http:80,https:443,http:10001)3个端口使用",
            prop: "remoteDomain",
            suffix: "http/s://",
            append: '.frp.chrelyonly.cn',
            display: false
          },
          {
            label: "命令行日志",
            prop: "msg",
            type:"input",
            readonly: true,
            display: false,
            span: 24,
          },
          {
            label: "当前状态",
            prop: "status",
            span: 24,
            value: "4",
            type:"input",
          },
          {
            label: "外网地址",
            prop: "url",
            span: 24,
            type:"input",
          },
        ]
      }
    }
  },

  methods:{
    switchClient(form,done){
      // 存下缓存
      localStorage.setItem("proxyInfo",JSON.stringify(form));
      let params = " ";
      params += "--proxyName " + form.proxyName + " ";
      params += "--proxyType " + form.proxyType + " ";
      if (form.proxyType === "1"){
        params += "--proxyAddr " + form.proxyAddr + " ";
        params += "--proxyPort " + form.proxyPort + " ";
        params += "--remotePort " + form.remotePort + " ";
      }else if (form.proxyType === "2"){
        params += "--proxyAddr " + form.proxyAddr + " ";
        params += "--proxyPort " + form.proxyPort + " ";
        params += "--remoteDomain " + form.remoteDomain + " ";
      }else
      if (form.proxyType === "3"){
        params += "--dir " + form.dir + " ";
        params += "--tcpOrHttp " + form.tcpOrHttp + " ";
        if (form.tcpOrHttp === "1") {
          params += "--remoteDomain " + form.remoteDomain + " ";
        }else {
          params += "--remotePort " + form.remotePort + " ";
        }
      }else if (form.proxyType === "4"){
        params += "--domain " + form.domain + " ";
        params += "--remoteDomain " + form.remoteDomain + " ";
      }
      ipc.invoke(ipcApiRoute.openSoftware, "frp-client.exe " + params).then(result => {
        console.log(result)
        console.log(result)
        console.log(result)
        if (!result) {
          this.$message.error('程序不存在');
          this1.form.msg = "已停止"
          this1.form.status = "4"
          this1.option.submitText = "开始连接"
          frpProcess = null;
          done();
          return
        }
        if (result === "0"){
          this1.$message.success("已关闭连接")
          this1.form.msg = "已停止"
          this1.form.status = "4"
          this1.option.submitText = "开始连接"
          frpProcess = null;
          done();
          return;
        }
        this1.form.status = "1"
        this1.option.submitText = "关闭连接"
        let append  = this1.findObject(this1.option.column,"remoteDomain").append;
        this1.form.url = "https://" + this1.form.remoteDomain + append
        done();
      })
    },
  },
}

</script>
<template>

  <el-row :gutter="24">
    <el-col :span="24">
      <avue-form :option="option"  ref="formData" v-model="form" @submit="switchClient">
        <template #msg="scope">
          <div style="width: 100%;background-color:#000000;color: #ffffff;height: 200px;
          overflow-y: auto;border: 1px #000000 solid;
          border-radius: 10px;padding: 10px" v-html="scope.value"></div>
        </template>
        <template #status="scope">
          <el-tag v-if="scope.value === '1'" type="success">连接成功</el-tag>
          <el-tag v-else-if="scope.value === '2'" type="danger">连接失败</el-tag>
          <el-tag v-else-if="scope.value === '3'" type="warning">连接中</el-tag>
          <el-tag v-else type="info">未连接</el-tag>
        </template>
        <template #url="scope">
          <el-tag type="success">{{ scope.value }}</el-tag>
        </template>
      </avue-form>
    </el-col>
  </el-row>
</template>

<style scoped>

</style>